<?php

class ForeignKey {

	var $sName;
	var $oFromTable;
	var $aFromColumnList;
	var $oToTable;
	var $bMandatory;
	
	function ForeignKey ( $sName, & $oFromTable, $aFromColumnListAsStrings, & $oToTable, $bMandatory ) {

		$this->sName = $sName;

		$this->oFromTable  =& $oFromTable;
		if ( is_object( $oFromTable ) ) {
			$this->aFromColumnList = $oFromTable->AddForeignKeyFromThisTable( $this, $aFromColumnListAsStrings );
		}

		$this->oToTable    =& $oToTable;
		if ( is_object( $oToTable ) ) {
			$oToTable->AddForeignKeyReferencingThisTable( $this );
		}

		$this->bMandatory  = $bMandatory;
	}
	
	function GetName() {
		return $this->sName;	
	}

	function GetFromTableName() {
		if ( is_object( $this->oFromTable ) ) {
			return $this->oFromTable->GetName();
		}
		return $this->oFromTable;
	}

	function GetToTableName() {
		if ( is_object( $this->oToTable ) ) {
			return $this->oToTable->GetName();
		}
		return $this->oToTable;
	}
	
	function GetColumnNames() {
		
		$aColumnList = array();
		if ( is_array( $this->aFromColumnList ) ) {
			foreach ( $this->aFromColumnList as $oColumn ) {
				$aColumnList[] = $oColumn->GetName();
			}
		} else {
			$aColumnList = '';	
		}
		return $aColumnList;
	}
	
	function IsMandatory() {
		return $this->bMandatory == true;
	}

	function IsFromAFilteredTable() {
		return !is_object( $this->oFromTable );	
	}

	function IsToAFilteredTable() {
		return !is_object( $this->oToTable );	
	}
	
	function ReferencesAFilteredTable() {
		return $this->IsFromAFilteredTable() || $this->IsToAFilteredTable();
	}
}

?>
